package model
{	
	public class Outfit
	{
	    private var _style:String;
	    private var _clothes:Array = new Array();
		
	    ////////////////////////////////////////////////
	    // Public methods
	    
		public function Outfit(style:String) {
			_style = style;
		}
		
		public function get style():String { return _style; }
		public function get clothes():Array { return _clothes; }
		
		public function setStyle(s:String):void {
			_style = s;
		}
		
		public function contains(c:Cloth):Boolean {
			for(var i:Number=0;i<_clothes.length;i++){
				var cloth:Cloth = _clothes[i];
				if (cloth == c) {
					return true;
				}
			}
			return false;
		}
		
		public function isCloneOf(o:Outfit):Boolean {
			if (o.clothes.length == this.clothes.length) {
				for(var i:Number=0;i<_clothes.length;i++){
					var cloth:Cloth = _clothes[i];
					if (!(o.contains(cloth))) {
						return false;
					}
				}
				return true;
			} else {
				return false;
			}
		}
		
		public function addCloth(c:Cloth):Cloth {
			for(var i:Number=0;i<_clothes.length;i++){
				if (_clothes[i].type == c.type) {
					var removedC:Cloth = _clothes[i];
					removeCloth(_clothes[i]);
					_clothes.push(c);
					return removedC;
				}
			}
			
			_clothes.push(c);
			return null;
		}
		
		public function removeCloth(c:Cloth):Boolean {
			if ( this.contains(c) ) {
				_clothes.splice(_clothes.indexOf(c), 1);
				return true;
			} else {
				return false;
			}
		}
		
		public static function findSource(style:String, s:String) {
			return "assets/outfits/" + style + "_" + s + ".png";
		}
		
		public function clone():Outfit {
			var result:Outfit = new Outfit(_style);
			for(var i:Number=0;i<_clothes.length;i++) {
				result.addCloth(_clothes[i]);
			}
			return result;
		}
		
		private function containsClothOfType(t:String):Boolean {
			for(var i:Number=0;i<_clothes.length;i++){
				var cloth:Cloth = _clothes[i];
				if (cloth.type == t) {
					return true;
				}
			}
			return false;
		}
		
		public function isValid():String {
			
			var results:Array = new Array();
			if (!(this.containsClothOfType('pants'))) { results.push('pants'); }
			if (!(this.containsClothOfType('shirt'))) { results.push('shirts'); }
			if (!(this.containsClothOfType('shoes'))) { results.push('shoes'); }
			
			//------------------------
			if (results.length == 0) { return null; }
			
			var txt:String = "";
			if (results.length == 1) {
				txt = 'any ' + results[0].toString();
			} else if (results.length == 2) {
				txt = 'any ' + results[0].toString() + ' or ' + results[1].toString();
			} else if (results.length == 3) {
				txt = 'any ' + results[0].toString() + ', ' + results[1].toString() + ' or ' + results[2].toString();
			}
			
			return "Your outfit does not have " + txt + "! Are you sure that you want to continue saving?";	
		}
	}
}